home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zmisc1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.9 KB  |  174 lines

  1. /* Copyright (C) 1994, 1997, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zmisc1.c,v 1.3 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Miscellaneous Type 1 font operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gscrypt1.h"
  25. #include "stream.h"        /* for getting state of PFBD stream */
  26. #include "strimpl.h"
  27. #include "sfilter.h"
  28. #include "idict.h"
  29. #include "idparam.h"
  30. #include "ifilter.h"
  31.  
  32. /* <state> <from_string> <to_string> .type1encrypt <new_state> <substring> */
  33. /* <state> <from_string> <to_string> .type1decrypt <new_state> <substring> */
  34. private int type1crypt(P2(i_ctx_t *,
  35.             int (*)(P4(byte *, const byte *, uint, ushort *))));
  36. private int
  37. ztype1encrypt(i_ctx_t *i_ctx_p)
  38. {
  39.     return type1crypt(i_ctx_p, gs_type1_encrypt);
  40. }
  41. private int
  42. ztype1decrypt(i_ctx_t *i_ctx_p)
  43. {
  44.     return type1crypt(i_ctx_p, gs_type1_decrypt);
  45. }
  46. private int
  47. type1crypt(i_ctx_t *i_ctx_p,
  48.        int (*proc)(P4(byte *, const byte *, uint, ushort *)))
  49. {
  50.     os_ptr op = osp;
  51.     crypt_state state;
  52.     uint ssize;
  53.  
  54.     check_type(op[-2], t_integer);
  55.     state = op[-2].value.intval;
  56.     if (op[-2].value.intval != state)
  57.     return_error(e_rangecheck);    /* state value was truncated */
  58.     check_read_type(op[-1], t_string);
  59.     check_write_type(*op, t_string);
  60.     ssize = r_size(op - 1);
  61.     if (r_size(op) < ssize)
  62.     return_error(e_rangecheck);
  63.     discard((*proc)(op->value.bytes, op[-1].value.const_bytes, ssize,
  64.             &state));    /* can't fail */
  65.     op[-2].value.intval = state;
  66.     op[-1] = *op;
  67.     r_set_size(op - 1, ssize);
  68.     pop(1);
  69.     return 0;
  70. }
  71.  
  72. /* Get the seed parameter for eexecEncode/Decode. */
  73. /* Return npop if OK. */
  74. private int
  75. eexec_param(os_ptr op, ushort * pcstate)
  76. {
  77.     int npop = 1;
  78.  
  79.     if (r_has_type(op, t_dictionary))
  80.     ++npop, --op;
  81.     check_type(*op, t_integer);
  82.     *pcstate = op->value.intval;
  83.     if (op->value.intval != *pcstate)
  84.     return_error(e_rangecheck);    /* state value was truncated */
  85.     return npop;
  86. }
  87.  
  88. /* <target> <seed> eexecEncode/filter <file> */
  89. /* <target> <seed> <dict_ignored> eexecEncode/filter <file> */
  90. private int
  91. zexE(i_ctx_t *i_ctx_p)
  92. {
  93.     os_ptr op = osp;
  94.     stream_exE_state state;
  95.     int code = eexec_param(op, &state.cstate);
  96.  
  97.     if (code < 0)
  98.     return code;
  99.     return filter_write(i_ctx_p, code, &s_exE_template, (stream_state *)&state, 0);
  100. }
  101.  
  102. /* <source> <seed> eexecDecode/filter <file> */
  103. /* <source> <dict> eexecDecode/filter <file> */
  104. private int
  105. zexD(i_ctx_t *i_ctx_p)
  106. {
  107.     os_ptr op = osp;
  108.     stream_exD_state state;
  109.     int code;
  110.  
  111.     (*s_exD_template.set_defaults)((stream_state *)&state);
  112.     if (r_has_type(op, t_dictionary)) {
  113.     uint cstate;
  114.  
  115.     check_dict_read(*op);
  116.     if ((code = dict_uint_param(op, "seed", 0, 0xffff, 0x10000,
  117.                     &cstate)) < 0 ||
  118.         (code = dict_int_param(op, "lenIV", 0, max_int, 4,
  119.                    &state.lenIV)) < 0
  120.         )
  121.         return code;
  122.     state.cstate = cstate;
  123.     code = 1;
  124.     } else {
  125.     code = eexec_param(op, &state.cstate);
  126.     }
  127.     if (code < 0)
  128.     return code;
  129.     /*
  130.      * If we're reading a .PFB file, let the filter know about it,
  131.      * so it can read recklessly to the end of the binary section.
  132.      */
  133.     if (r_has_type(op - 1, t_file)) {
  134.     stream *s = (op - 1)->value.pfile;
  135.  
  136.     if (s->state != 0 && s->state->template == &s_PFBD_template) {
  137.         stream_PFBD_state *pss = (stream_PFBD_state *)s->state;
  138.  
  139.         state.pfb_state = pss;
  140.         /*
  141.          * If we're reading the binary section of a PFB stream,
  142.          * avoid the conversion from binary to hex and back again.
  143.          */
  144.         if (pss->record_type == 2) {
  145.         /*
  146.          * The PFB decoder may have converted some data to hex
  147.          * already.  Convert it back if necessary.
  148.          */
  149.         if (pss->binary_to_hex && sbufavailable(s) > 0) {
  150.             state.binary = 0;    /* start as hex */
  151.             state.hex_left = sbufavailable(s);
  152.         } else {
  153.             state.binary = 1;
  154.         }
  155.         pss->binary_to_hex = 0;
  156.         }
  157.         state.record_left = pss->record_left;
  158.     }
  159.     }
  160.     return filter_read(i_ctx_p, code, &s_exD_template, (stream_state *)&state, 0);
  161. }
  162.  
  163. /* ------ Initialization procedure ------ */
  164.  
  165. const op_def zmisc1_op_defs[] =
  166. {
  167.     {"3.type1encrypt", ztype1encrypt},
  168.     {"3.type1decrypt", ztype1decrypt},
  169.     op_def_begin_filter(),
  170.     {"2eexecEncode", zexE},
  171.     {"2eexecDecode", zexD},
  172.     op_def_end(0)
  173. };
  174.